package  
{
		
	import flash.display.InteractiveObject;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.Sfx;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.FP;
	/**
	 * ...
	 * @author Donatello
	 */
	public class Bomberman extends Entity
	{
		[Embed(source = "img/bomberman1.png" )] private const PLAYER_ANIM:Class;
		private var playerAnim:Spritemap;	

		protected var axisX:Number;// 1 o -1 
		protected var axisY:Number;// 1 o -1
		protected var antX:Number;
		protected var antY:Number;
		private var pressed:Boolean = false;
		
		private var cellWidth:Number = 32;
		private var cellHeight:Number = 64;
		
		protected var speed:Number =2;
		public var ammo:int=50;
		protected var colisiona:Boolean = false;
		
		
		public function Bomberman(px:Number,py:Number) 
		{
			playerAnim = new Spritemap(PLAYER_ANIM, cellWidth,cellHeight);
			playerAnim.add("idleUp", [8], 10, false);
			playerAnim.add("idleDown", [12], 10, false);
			playerAnim.add("idleLeft", [0], 10, false);
			playerAnim.add("idleRight", [4], 10, false);
			
			playerAnim.add("up", [8,9,10], 10, true);
			playerAnim.add("down", [12,13,14], 10, true);
			playerAnim.add("left", [0,1,2], 10, true);
			playerAnim.add("right", [4,5,6], 10, true);
			
			
			graphic = playerAnim;
		
			
			playerAnim.play("idleUp");
			
			playerAnim.alpha = 0;
			
			setHitbox(cellWidth,cellHeight);
			
			x = px;
			y = py;
			
			axisX = 0;
			axisY = -1;
			
			type = "Bomberman";
			
		}
		
		public function move(fx:Number,fy:Number):void
		{
			axisX = fx;
			axisY = fy;
			
			if (!colisiona)
			{
				antX = x;
				antY = y;
				
				if ((x + fx * speed) > 45 &&  (x + fx * speed)+32 < 703)
					x += fx *speed;
				
				if ((y + fy * speed) > 18 &&  (y + fy * speed)+58 < 582)
					y += fy * speed;
			}
			else
			{
				x = antX;
				y = antY;
			}
		}
		override public function update():void
		{
			
			if (collide("Wall", x, y))
			{
				colisiona = true;
			}
			else
				colisiona = false;
			
			
			if (Input.check(Key.UP) )
			{
				move(0, -1);
				playerAnim.play("up")
			}
			else if(Input.check(Key.DOWN) )
			{
				move(0, 1);
				playerAnim.play("down")
			}
			else if(Input.check(Key.LEFT) )
			{
				move(-1, 0);
				playerAnim.play("left")
			}
			else if(Input.check(Key.RIGHT) )
			{
				move(1, 0);
				playerAnim.play("right")
			}
			else
			{	
				if (axisX == -1)  playerAnim.play("idleLeft");
				if (axisX == 1)  playerAnim.play("idleRight");
				if (axisY == -1) playerAnim.play("idleUp");
				if (axisY == 1) playerAnim.play("idleDown");
				
				
				if(ammo>0)
					checkShoot();
			}
			

		}
		
		
		private function checkShoot():void
		{
			if (Input.check(Key.SPACE))
			{	if (!pressed)
				{	
					FP.world.add(new Bomb(x, y+15));
					pressed = true;
					ammo -= 1;
				}
			}
			else
				pressed = false;
		}
		
		public function changeAlpha(a:Boolean):void
		{
			if(a)
				playerAnim.alpha = 1;
			else
				playerAnim.alpha = 0;
		}
	}

}